/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desu.nya.DAO;

import desu.db.HibernateUtil;
import desu.nya.Otaku;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

/**
 *
 * @author Fess
 */
public class OtakuIMPL implements OtakuDAO{

    @Override
    public Otaku addOtaku(Otaku otaku) throws SQLException {
        Session session=null;
        Otaku ot=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            List<Otaku> list=session.createCriteria(Otaku.class).add(Expression.eq("nickname", otaku.getNickname())).add(Expression.eq("login", otaku.getLogin())).list();
            if(list.isEmpty()){
                session.beginTransaction();
                session.save(otaku);
                session.getTransaction().commit();
                ot=otaku;
            }
            else
                System.err.println("HiB: Otaku with this login is already excist");
        }
        catch(Exception ex){
            System.err.println("HiB: add otaku fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return ot;
    }

    @Override
    public Otaku getOtakuByID(int id) throws SQLException {
        Session session=null;
        Otaku otaku=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            otaku=(Otaku)session.load(Otaku.class, id);
        }
        catch(Exception ex){
            System.out.println("HiB: get otaku fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return otaku;
    }

    @Override
    public void updateOtaku(int otaku_id, Otaku otaku) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(otaku);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("HiB: update otaku fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public List<String> getAllOtaku() throws SQLException {
        Session session=null;
        List<Otaku> listOtaku;
        List<String> listString=new ArrayList();
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            listOtaku=(List<Otaku>)session.createCriteria(Otaku.class).list();
            for(Otaku ot:listOtaku)
                listString.add(ot.getNickname());
        }
        catch(Exception ex){
            System.err.println("HiB: get otaku list fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return listString;
    }

    @Override
    public void deleteOtaku(Otaku otaku) throws SQLException {
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(otaku);
            session.getTransaction().commit();
        }
        catch(Exception ex){
            System.err.println("HiB: delete otaku fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
    }

    @Override
    public Otaku login(String log, String pass) {
        String ret=null;
        Otaku ot=null;
        Session session=null;
        try{
            session=HibernateUtil.getSessionFactory().openSession();
            String hashPass=hashing(pass);
            List<Otaku> list=session.createQuery("from Otaku where login=:log AND pass=:pass").setString("log", log).setString("pass", hashPass).list();
            if(!list.isEmpty()){
                ot=list.get(0);
                ret=ot.getNickname();
            }
        }
        catch(Exception ex){
            System.err.println("HiB: login fail");
        }
        finally{
            if(session!=null&&session.isOpen())
                session.close();
        }
        return ot;
    }
    
    private String hashing(String strForHash){
        if(strForHash==null)return null;
        String ret=null;
        Session session=null;
        try {
            MessageDigest md=MessageDigest.getInstance("MD5");
            byte[] buf=strForHash.getBytes();
            md.update(buf);
            byte[] mdb=md.digest();
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<mdb.length;i++){
                String hex=Integer.toHexString(0xf*mdb[i]);
                if(hex.length()==1)sb.append('0');
                sb.append(hex);
            }
            System.err.println("MD5: "+sb);
            ret=sb.toString();
        } catch (NoSuchAlgorithmException ex) {
            
        } 
        return ret;
    }
}
